package com.fyy.netty.chapter02.c2;

import com.fyy.netty.utils.ByteBufferUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;

/**
 * @Author fyy
 * @Description
 * @Date 2021/5/8 10:34
 */
public class SliceTest {

    public static void main(String[] args) {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(10);
        buf.writeBytes(new byte[]{'a','b','c','d','e','f','j','h','i'});
        ByteBufferUtil.log(buf);

        //使用slice实现零拷贝切片，且切片后容量不可变
        ByteBuf f1 = buf.slice(0, 5);
        ByteBuf f2 = buf.slice(5, 5);
        ByteBufferUtil.log(f1);
        ByteBufferUtil.log(f2);

        ByteBuf buf1 = ByteBufAllocator.DEFAULT.buffer(10);
        buf.writeBytes(new byte[]{'1','2','3','4','5'});
        ByteBuf buf2 = ByteBufAllocator.DEFAULT.buffer(10);
        buf.writeBytes(new byte[]{'6','7','8','9','0'});

        //使用CompositeByteBuf组合多个小的ByteBuf,同样基于零拷贝
        CompositeByteBuf bytebuf = ByteBufAllocator.DEFAULT.compositeBuffer();
        bytebuf.addComponent(true,buf1);
        bytebuf.addComponent(true,buf2);

        bytebuf.addComponents(true,buf1,buf2);



    }

}
